package com.gy.ai.agent.platform.dashscope;

import com.alibaba.fastjson2.JSON;
import com.gy.ai.agent.BasicPlatform;
import com.gy.ai.agent.client.BaseReqClient;
import com.gy.ai.agent.client.SseClient;
import com.gy.ai.agent.core.BaseResponse;
import com.gy.ai.agent.core.ReqContext;
import com.gy.ai.agent.core.ReqOptions;
import com.gy.ai.agent.listener.BaseReqClientListener;
import com.gy.ai.agent.listener.ReqClientListener;
import com.gy.ai.agent.listener.StreamRespListener;
import com.gy.ai.agent.platform.dashscope.entity.*;
import com.gy.ai.agent.util.SysOkHttpClientUtil;

import java.util.Collections;
import java.util.List;

/**
 * @author LGY
 * Create by 2025/6/19 16:58
 */
public class DashscopePlatform extends BasicPlatform<DashscopePlatformConfig> {

    // 创建文本对话请求
    public static final String TEXT_CHART = "/chat/completions";
    // 创建文本向量请求 （嵌入请求）
    public static final String TEXT_EMBEDDINGS = "/embeddings";
    // 创建文本重排序请求
    public static final String TEXT_RERANK = "/services/rerank/text-rerank/text-rerank";
    // 创建文生图的请求
    public static final String TEXT_TO_IMAGE = "/services/aigc/text2image/image-synthesis";

    public DashscopePlatform(DashscopePlatformConfig config) {
        super(config);
    }

    @Override
    public DashscopePlatformChatResp chat(String prompt) {
        DashscopePlatformChatReq dashscopePlatformChatReq = new DashscopePlatformChatReq();
        dashscopePlatformChatReq.setMessages(Collections.singletonList(new DashscopePlatformChatReq.Message().setRole("user").setContent(prompt)));
        return chat(dashscopePlatformChatReq);
    }

    @Override
    public DashscopePlatformChatResp chat(ReqOptions options) {
        DashscopePlatformConfig dashscopePlatformConfig = this.getConfig();
        String url = dashscopePlatformConfig.getEndpoint() + TEXT_CHART;
        // 请求参数
        DashscopePlatformChatReq req = (DashscopePlatformChatReq) options;
        req.setModel(dashscopePlatformConfig.getModel())
                .setStream(false);
        // 发起请求
        return SysOkHttpClientUtil.doPost(url, dashscopePlatformConfig.getHeaders(), JSON.toJSONString(req), DashscopePlatformChatResp.class);
    }

    @Override
    public void chatStream(String prompt, StreamRespListener streamResponseListener) {
        DashscopePlatformChatReq dashscopePlatformChatReq = new DashscopePlatformChatReq();
        dashscopePlatformChatReq.setMessages(Collections.singletonList(new DashscopePlatformChatReq.Message().setRole("user").setContent(prompt)));
        chatStream(dashscopePlatformChatReq, streamResponseListener);
    }

    @Override
    public void chatStream(ReqOptions options, StreamRespListener streamResponseListener) {
        DashscopePlatformConfig dashscopePlatformConfig = this.getConfig();
        BaseReqClient reqClient = new SseClient();
        String url = dashscopePlatformConfig.getEndpoint() + TEXT_CHART;
        DashscopePlatformChatReq req = (DashscopePlatformChatReq) options;
        req.setModel(dashscopePlatformConfig.getModel())
                .setStream(true);
        ReqClientListener listener = new BaseReqClientListener(this, streamResponseListener, reqClient, new ReqContext(this, reqClient));
        reqClient.start(url, dashscopePlatformConfig.getHeaders(), JSON.toJSONString(req), listener);
    }

    @Override
    public DashscopePlatformEmbeddingResp embedding(String input) {
        DashscopePlatformEmbeddingReq dashscopePlatformChatReq = new DashscopePlatformEmbeddingReq();
        dashscopePlatformChatReq.setInput(Collections.singletonList(input))
                .setEncoding_format("float");
        return embedding(dashscopePlatformChatReq);
    }

    @Override
    public DashscopePlatformEmbeddingResp embedding(ReqOptions options) {
        DashscopePlatformConfig dashscopePlatformConfig = this.getConfig();
        String url = dashscopePlatformConfig.getEndpoint() + TEXT_EMBEDDINGS;
        // 请求参数
        DashscopePlatformEmbeddingReq req = (DashscopePlatformEmbeddingReq) options;
        req.setModel(dashscopePlatformConfig.getModel());
        // 发起请求
        return SysOkHttpClientUtil.doPost(url, dashscopePlatformConfig.getHeaders(), JSON.toJSONString(req), DashscopePlatformEmbeddingResp.class);
    }

    @Override
    public DashscopePlatformRerankResp rerank(String query, List<String> documents) {
        DashscopePlatformRerankReq dashscopePlatformRerankReq = new DashscopePlatformRerankReq();
        DashscopePlatformRerankReq.Input input = new DashscopePlatformRerankReq.Input();
        input.setQuery(query)
                .setDocuments(documents);
        dashscopePlatformRerankReq.setInput(input);
        return rerank(dashscopePlatformRerankReq);
    }

    @Override
    public DashscopePlatformRerankResp rerank(ReqOptions options) {
        DashscopePlatformConfig dashscopePlatformConfig = this.getConfig();
        String url = dashscopePlatformConfig.getEndpoint() + TEXT_RERANK;
        // 请求参数
        DashscopePlatformRerankReq req = (DashscopePlatformRerankReq) options;
        req.setModel(dashscopePlatformConfig.getModel());
        // 发起请求
        return SysOkHttpClientUtil.doPost(url, dashscopePlatformConfig.getHeaders(), JSON.toJSONString(req), DashscopePlatformRerankResp.class);
    }

    @Override
    public BaseResponse generateImage(String prompt) {
        return generateImage(ReqOptions.DEFAULT);
    }

    @Override
    public BaseResponse generateImage(ReqOptions options) {
        throw new RuntimeException("暂不支持同步生成图片");
    }
}
